package com.cq.hd.app.biz;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import com.cq.hd.app.dto.OrderExportDto;
import com.cq.hd.common.constant.Constant;
import com.cq.hd.common.enums.ResponseEnums;
import com.cq.hd.common.enums.biz.ActiveEnrollInfoTypeEnum;
import com.cq.hd.common.enums.biz.ActiveTypeEnum;
import com.cq.hd.common.enums.biz.SubOrderStateEnum;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.common.response.Throw;
import com.cq.hd.common.utils.GenerateUtil;
import com.cq.hd.common.utils.LocalDateUtil;
import com.cq.hd.common.utils.StringUtils;
import com.cq.hd.member.api.TbBusinessApi;
import com.cq.hd.member.api.vo.BusinessPageVo;
import com.cq.hd.order.api.TbOrderApi;
import com.cq.hd.order.api.dto.ActiveEnrollInfoOptionDto;
import com.cq.hd.order.api.dto.ActiveEnrollInfoSonDto;
import com.cq.hd.order.api.dto.AdminOrderPageDto;
import com.cq.hd.order.api.excel.*;
import com.cq.hd.order.api.vo.AdminOrderItemVo;
import com.cq.hd.order.api.vo.AdminOrderPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@Slf4j
public class OrderBiz {

    @Autowired
    private TbBusinessApi businessApi;

    @Autowired
    private TbOrderApi orderApi;

    public Result<Object> exportOrder(OrderExportDto orderExportDto, HttpServletResponse response) {
        Long activeId = orderExportDto.getActiveId();
        Integer orderType = orderExportDto.getOrderType();
        // 积分订单，导出不需要选择活动
        if (orderType == null || !orderType.equals(ActiveTypeEnum.POINTS.getValue())) {
            if (activeId == null || activeId <= 0) {
                Throw.isBusinessException("请选择活动");
            }
        }

        // 日期类型：1-近1个月，2-今日，3-昨日，4-本周，5-自定义日期
        Integer type = orderExportDto.getType();
        if (type == null) {
            Throw.isBusinessException("请选择日期");
        }

        LocalDate now = LocalDate.now();
        String orderTimeStart = orderExportDto.getOrderTimeStart();
        String orderTimeEnd = orderExportDto.getOrderTimeEnd();

        if (type == 1) {
            LocalDate localDate = now.plusMonths(-1);
            String beforeLocalDate = LocalDateUtil.toLocalDateToString(localDate, Constant.YYYY_MM_DD);
            String endLocalDate = LocalDateUtil.toLocalDateToString(now, Constant.YYYY_MM_DD);
            orderTimeStart = beforeLocalDate + " 00:00:00";
            orderTimeEnd = endLocalDate + " 23:59:59";
        } else if (type == 2) {
            String beforeLocalDate = LocalDateUtil.toLocalDateToString(now, Constant.YYYY_MM_DD);
            orderTimeStart = beforeLocalDate + " 00:00:00";
            orderTimeEnd = beforeLocalDate + " 23:59:59";
        } else if (type == 3) {
            LocalDate localDate = now.plusDays(-1);
            String beforeLocalDate = LocalDateUtil.toLocalDateToString(localDate, Constant.YYYY_MM_DD);
            orderTimeStart = beforeLocalDate + " 00:00:00";
            orderTimeEnd = beforeLocalDate + " 23:59:59";
        } else if (type == 4) {
            // 本周开始日期
            String mondayStartStr = LocalDateUtil.toLocalDateToString(now.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)), Constant.YYYY_MM_DD);
            // 本周结束日期
            String sundayEndStr = LocalDateUtil.toLocalDateToString(now.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)), Constant.YYYY_MM_DD);

            orderTimeStart = mondayStartStr + " 00:00:00";
            orderTimeEnd = sundayEndStr + " 23:59:59";
        } else if (type == 5) {
            // 如果是自定义日期，间隔不能超过31天
            LocalDateTime startLocalDateTime = null;
            LocalDateTime endLocalDateTime = null;
            try {
                startLocalDateTime = LocalDateUtil.toLocalDateTime(orderTimeStart, Constant.YYYY_MM_DD_HH_MM_SS);
                endLocalDateTime = LocalDateUtil.toLocalDateTime(orderTimeEnd, Constant.YYYY_MM_DD_HH_MM_SS);
            } catch (Exception e) {
                Throw.isBusinessException("日期格式错误");
            }

            if (endLocalDateTime.isBefore(startLocalDateTime)) {
                Throw.isBusinessException("结束时间不能晚于开始时间");
            }

            long seconds = LocalDateUtil.getDurationToLocalDateTime(startLocalDateTime, endLocalDateTime).getSeconds();
            if (seconds > 0) {
                long days = seconds / 60 / 60 / 24;
                long remain = seconds % 60 % 60 % 24;
                if (remain > 0) {
                    days += 1;
                }

                if (days > 31) {
                    Throw.isBusinessException("自定义时间跨度不能超过31天");
                }
            }
        }

        Long businessId = orderExportDto.getBusinessId();
        Long userId = orderExportDto.getUserId();
        if (businessId == null || businessId <= 0) {
            if (userId == null || userId <= 0) {
                Throw.isBusinessException(ResponseEnums.USER_ACCESS_DENIED);
            }

            // 根据userId查询商家id
            BusinessPageVo businessPageVo = businessApi.getByUserId(userId).unpack();
            if (businessPageVo == null) {
                // 如果不是商家，则返回空
                Throw.isBusinessException(ResponseEnums.BUSINESS_ACCESS_DENIED);
            }

            businessId = businessPageVo.getId();
        }

        // 根据活动id+商家id+日期查询订单数据
        AdminOrderPageDto adminOrderPageDto = new AdminOrderPageDto();
        adminOrderPageDto.setActiveId(activeId);
        adminOrderPageDto.setBusinessId(businessId);
        adminOrderPageDto.setOrderTimeStart(orderTimeStart);
        adminOrderPageDto.setOrderTimeEnd(orderTimeEnd);
        if (orderType != null && orderType > 0) {
            adminOrderPageDto.setOrderType(orderType);
        }
        List<AdminOrderPageVo> list = orderApi.listAdminOrder(adminOrderPageDto).unpack();
        if (CollectionUtils.isEmpty(list)) {
            Throw.isBusinessException("选择的时段内没有订单");
        }

        // 预检查标识：0-需要，1-不需要
        Integer checkState = orderExportDto.getCheckState();
        if (checkState != null && checkState == 0) {
            return ResultGenerator.success();
        }

        if (orderType == null || orderType <= 0) {
            orderType = list.get(0).getOrderType();
        }
        if (ActiveTypeEnum.GROUP.getValue().equals(orderType)) {
            // 团购活动订单，拼装团购活动订单excel模板sheet
            exportGroupOrder(list, response);
            return null;
        }
        if (ActiveTypeEnum.ENROLL.getValue().equals(orderType)) {
            // 报名活动订单，拼装报名活动订单excel模板sheet
            exportEnrollOrder(list, response);
            return null;
        }
        if (ActiveTypeEnum.POINTS.getValue().equals(orderType)) {
            exportPointsOrder(list, response);
            return null;
        }

        return null;
    }

    private void exportPointsOrder(List<AdminOrderPageVo> list, HttpServletResponse response) {
        // 组装订单明细sheet
        List<PointsOrderSheet> pointsOrderSheets = new ArrayList<>();
        // 组装订单汇总sheet
        List<TotalPointsOrderSheet> totalPointsOrderSheets = new ArrayList<>();
        setPointsOrderData(list, pointsOrderSheets, totalPointsOrderSheets);

        LocalDateTime nowLocalDateTime = LocalDateUtil.getNowLocalDateTime();
        String fileName = "积分订单" + LocalDateUtil.toLocalDateTimeToString(nowLocalDateTime, "yyyyMMddHHmmss") + GenerateUtil.getRandomNum(6);

        // 多个sheet的输出需要使用ExcelWriter类，这里想要下载成功，需要输出到OutputStream中
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).inMemory(true).build()) {
            // 设置响应类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            // 设置编码格式
            response.setCharacterEncoding("utf-8");
            // 设置跨域
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD, PUT");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "*");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");

            List<List<String>> mergeColDataList = Stream.of(pointsOrderSheets.stream().map(PointsOrderSheet::getOrderNo).collect(Collectors.toList())
            ).collect(Collectors.toList());
            List<Integer> mergeColIndexList = Stream.of(0).collect(Collectors.toList());

            WriteSheet pointsOrderSheet = EasyExcel.writerSheet(0, "订单明细")
                    .registerWriteHandler(new CustomWriteHandler())
                    .registerWriteHandler(new CustomCellMergeStrategy(mergeColDataList, mergeColIndexList))
                    .head(PointsOrderSheet.class).build();
            excelWriter.write(pointsOrderSheets, pointsOrderSheet);
            WriteSheet pointsOrderSummarySheet = EasyExcel.writerSheet(1, "订单汇总")
                    .registerWriteHandler(new CustomWriteHandler()).head(TotalPointsOrderSheet.class).build();
            excelWriter.write(totalPointsOrderSheets, pointsOrderSummarySheet);
        } catch (Exception ioException) {
            ioException.printStackTrace();
        }
    }

    private void exportEnrollOrder(List<AdminOrderPageVo> list, HttpServletResponse response) {
        // 组装订单明细sheet
        List<EnrollOrderSheet> enrollOrderSheets = new ArrayList<>();
        // 组装订单汇总sheet
        List<TotalEnrollOrderSheet> totalEnrollOrderSheets = new ArrayList<>();
        setEnrollOrderData(list, enrollOrderSheets, totalEnrollOrderSheets);

        LocalDateTime nowLocalDateTime = LocalDateUtil.getNowLocalDateTime();
        String fileName = "订单" + LocalDateUtil.toLocalDateTimeToString(nowLocalDateTime, "yyyyMMddHHmmss") + GenerateUtil.getRandomNum(6);

        // 多个sheet的输出需要使用ExcelWriter类，这里想要下载成功，需要输出到OutputStream中
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).inMemory(true).build()) {
            // 设置响应类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            // 设置编码格式
            response.setCharacterEncoding("utf-8");
            // 设置跨域
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD, PUT");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "*");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");

            List<List<String>> mergeColDataList = Stream.of(enrollOrderSheets.stream().map(EnrollOrderSheet::getOrderNo).collect(Collectors.toList())
            ).collect(Collectors.toList());
            List<Integer> mergeColIndexList = Stream.of(0).collect(Collectors.toList());

            WriteSheet enrollOrderSheet = EasyExcel.writerSheet(0, "订单明细")
                    .registerWriteHandler(new CustomWriteHandler())
                    .registerWriteHandler(new CustomCellMergeStrategy(mergeColDataList, mergeColIndexList))
                    .head(EnrollOrderSheet.class).build();
            excelWriter.write(enrollOrderSheets, enrollOrderSheet);
            WriteSheet enrollOrderSummarySheet = EasyExcel.writerSheet(1, "订单汇总")
                    .registerWriteHandler(new CustomWriteHandler()).head(TotalEnrollOrderSheet.class).build();
            excelWriter.write(totalEnrollOrderSheets, enrollOrderSummarySheet);
        } catch (Exception ioException) {
            ioException.printStackTrace();
        }
    }

    private void setPointsOrderData(List<AdminOrderPageVo> list, List<PointsOrderSheet> pointsOrderSheets, List<TotalPointsOrderSheet> totalPointsOrderSheets) {
        // 总订单数
        Integer orderNum = list.size();
        // 核销订单数
        int verifyOrderNum = 0;
        // 核销金额总计=核销的子订单实付金额（分销金额）
        BigDecimal verifyOrderAmt = BigDecimal.ZERO;
        // 商品总金额
        BigDecimal totalGoodsPrice = BigDecimal.ZERO;
        // 下单人数
        Integer orderUserNum = list.stream().map(AdminOrderPageVo::getUserId).collect(Collectors.toSet()).size();

        for (AdminOrderPageVo adminOrderPageVo : list) {
            // 是否是核销订单
            boolean isVerify = false;

            List<AdminOrderItemVo> adminOrderItems = adminOrderPageVo.getAdminOrderItems();
            for (AdminOrderItemVo adminOrderItem : adminOrderItems) {
                BigDecimal goodsPrice = adminOrderItem.getGoodsPrice();
                if (goodsPrice != null && goodsPrice.compareTo(BigDecimal.ZERO) > 0) {
                    totalGoodsPrice = totalGoodsPrice.add(goodsPrice);
                }

                Integer subOrderState = adminOrderItem.getSubOrderState();
                if (SubOrderStateEnum.VERIFIED.getValue().equals(subOrderState)) {
                    isVerify = true;
                    verifyOrderAmt = verifyOrderAmt.add(goodsPrice);
                }
            }

            if (isVerify) {
                verifyOrderNum += 1;
            }

            for (AdminOrderItemVo adminOrderItem : adminOrderItems) {
                PointsOrderSheet orderSheet = new PointsOrderSheet();
                orderSheet.setOrderNo(adminOrderPageVo.getOrderNo());
                orderSheet.setActiveTitle(adminOrderPageVo.getActiveTitle());
                orderSheet.setOrderNum(adminOrderPageVo.getOrderNum());

                orderSheet.setSubOrderNo(adminOrderItem.getSubOrderNo());
                orderSheet.setGoodsPrice(adminOrderItem.getGoodsPrice());
                orderSheet.setSubOrderState(adminOrderItem.getSubOrderState());
                orderSheet.setMerchantName(adminOrderItem.getMerchantShortName());
                orderSheet.setVerifyTime(adminOrderItem.getVerifyTime());

                orderSheet.setOrderNickName(adminOrderPageVo.getOrderNickName());
                orderSheet.setOrderMobile(adminOrderPageVo.getOrderMobile());
                orderSheet.setOrderPrice(adminOrderPageVo.getOrderPrice());
                orderSheet.setPayPrice(adminOrderPageVo.getPayPrice());
                orderSheet.setOrderState(adminOrderPageVo.getOrderState());

                orderSheet.setOrderTime(adminOrderPageVo.getOrderTime());
                orderSheet.setPayTime(adminOrderPageVo.getPayTime());
                orderSheet.setFinishTime(adminOrderPageVo.getFinishTime());

                pointsOrderSheets.add(orderSheet);
            }
        }

        TotalPointsOrderSheet totalPointsOrderSheet = new TotalPointsOrderSheet();
        totalPointsOrderSheet.setOrderNum(orderNum);
        totalPointsOrderSheet.setVerifyOrderNum(verifyOrderNum);
        totalPointsOrderSheet.setVerifyOrderAmt(verifyOrderAmt);
        totalPointsOrderSheet.setTotalOrderPrice(totalGoodsPrice);
        totalPointsOrderSheet.setOrderUserNum(orderUserNum);
        totalPointsOrderSheets.add(totalPointsOrderSheet);
    }

    private void setEnrollOrderData(List<AdminOrderPageVo> list, List<EnrollOrderSheet> enrollOrderSheets, List<TotalEnrollOrderSheet> totalEnrollOrderSheets) {
        // 总订单数
        Integer orderNum = list.size();
        // 核销订单数
        int verifyOrderNum = 0;
        // 核销金额总计=核销的子订单实付金额（分销金额）
        BigDecimal verifyOrderAmt = BigDecimal.ZERO;
        // 订单补贴合计=分账补贴累计
        BigDecimal totalOrderSubsidyAmt = BigDecimal.ZERO;
        // 佣金合计
        BigDecimal totalCommissionAmt = BigDecimal.ZERO;
        // 总补贴金额=分账补贴+佣金
        BigDecimal sumSubsidyAmt = BigDecimal.ZERO;
        // 总返现金额
        BigDecimal sumReturnAmt = BigDecimal.ZERO;
        // 下单人数
        Integer orderUserNum = list.stream().map(AdminOrderPageVo::getUserId).collect(Collectors.toSet()).size();

        for (AdminOrderPageVo adminOrderPageVo : list) {
            // 推广权益
            String promotionEquity = "";
            // 推广权益类型:0-无,1-其他权益,2-佣金 如果是其他权益,则取otherEquity字段数据,如果是佣金,则取actualCommissionAmt实际全核销佣金金额和totalCommissionAmt总实际佣金
            Integer promotionEquityType = adminOrderPageVo.getPromotionEquityType();
            if (promotionEquityType == 1) {
                promotionEquity = adminOrderPageVo.getOtherEquity();
            } else if (promotionEquityType == 2) {
                promotionEquity = adminOrderPageVo.getActualCommissionAmt().setScale(2, BigDecimal.ROUND_DOWN).toString();
            }

            // 商品本金合计=是所有商品原本的售价合计
            BigDecimal totalMerchantAmt = BigDecimal.ZERO;
            // 商品补贴合计=分账补贴累加
            BigDecimal subsidyAmt = BigDecimal.ZERO;
            // 订单总补贴=分账补贴累加+佣金
            BigDecimal totalOrderSubsidy = BigDecimal.ZERO;
            // 订单总返现
            BigDecimal totalReturnAmt = BigDecimal.ZERO;
            // 是否是核销订单
            boolean isVerify = false;

            List<AdminOrderItemVo> adminOrderItems = adminOrderPageVo.getAdminOrderItems();
            for (AdminOrderItemVo adminOrderItem : adminOrderItems) {
                BigDecimal goodsPrice = adminOrderItem.getGoodsPrice();
                if (goodsPrice != null && goodsPrice.compareTo(BigDecimal.ZERO) > 0) {
                    totalMerchantAmt = totalMerchantAmt.add(goodsPrice);
                }

                BigDecimal totalSubsidyAmt = adminOrderItem.getTotalSubsidyAmt();
                if (totalSubsidyAmt != null && totalSubsidyAmt.compareTo(BigDecimal.ZERO) > 0) {
                    subsidyAmt = subsidyAmt.add(totalSubsidyAmt);
                    totalOrderSubsidy = totalOrderSubsidy.add(totalSubsidyAmt);
                }

                BigDecimal commissionAmt = adminOrderItem.getCommissionAmt();
                if (commissionAmt != null && commissionAmt.compareTo(BigDecimal.ZERO) > 0) {
//                    totalOrderSubsidy = totalOrderSubsidy.add(commissionAmt);
                    subsidyAmt = subsidyAmt.subtract(commissionAmt);
                }

                Integer subOrderState = adminOrderItem.getSubOrderState();
                if (SubOrderStateEnum.VERIFIED.getValue().equals(subOrderState)) {
                    isVerify = true;
                    verifyOrderAmt = verifyOrderAmt.add(adminOrderItem.getDistributionOrderAmt());
                    totalReturnAmt = totalReturnAmt.add(adminOrderItem.getReturnAmt());
                }
            }

            if (isVerify) {
                verifyOrderNum += 1;
            }
            totalOrderSubsidyAmt = totalOrderSubsidyAmt.add(subsidyAmt);
            totalCommissionAmt = totalCommissionAmt.add(adminOrderPageVo.getActualCommissionAmt());
            sumSubsidyAmt = sumSubsidyAmt.add(totalOrderSubsidy);
            sumReturnAmt = sumReturnAmt.add(totalReturnAmt);


            for (AdminOrderItemVo adminOrderItem : adminOrderItems) {
                EnrollOrderSheet enrollOrderSheet = new EnrollOrderSheet();
                enrollOrderSheet.setOrderNo(adminOrderPageVo.getOrderNo());
                enrollOrderSheet.setActiveTitle(adminOrderPageVo.getActiveTitle());
                enrollOrderSheet.setOrderNum(adminOrderPageVo.getOrderNum());

                enrollOrderSheet.setSubOrderNo(adminOrderItem.getSubOrderNo());
                enrollOrderSheet.setGoodsPrice(adminOrderItem.getGoodsPrice());
                enrollOrderSheet.setMerchantAmt(adminOrderItem.getMerchantAmt());
                enrollOrderSheet.setTotalSubsidyAmt(adminOrderItem.getTotalSubsidyAmt());
                enrollOrderSheet.setSubOrderState(adminOrderItem.getSubOrderState());
                enrollOrderSheet.setMerchantName(adminOrderItem.getMerchantShortName());
                enrollOrderSheet.setVerifyTime(adminOrderItem.getVerifyTime());

                enrollOrderSheet.setOrderNickName(adminOrderPageVo.getOrderNickName());
                enrollOrderSheet.setOrderMobile(adminOrderPageVo.getOrderMobile());
                enrollOrderSheet.setOrderPrice(adminOrderPageVo.getOrderPrice());
                enrollOrderSheet.setDiscountAmt(adminOrderPageVo.getDiscountAmt());
                enrollOrderSheet.setRedPacketAmt(adminOrderPageVo.getRedPacketAmt());
                enrollOrderSheet.setPayPrice(adminOrderPageVo.getPayPrice());
                enrollOrderSheet.setAgentNickName(adminOrderPageVo.getAgentNickName());
                enrollOrderSheet.setAgentMobile(adminOrderPageVo.getAgentMobile());
                enrollOrderSheet.setOrderState(adminOrderPageVo.getOrderState());

                enrollOrderSheet.setOrderTime(adminOrderPageVo.getOrderTime());
                enrollOrderSheet.setPayTime(adminOrderPageVo.getPayTime());
                enrollOrderSheet.setFinishTime(adminOrderPageVo.getFinishTime());

                // 推广权益
                enrollOrderSheet.setPromotionEquity(promotionEquity);

                // 商品本金合计=是所有商品原本的售价合计
//                groupOrderSheet.setTotalMerchantAmt(totalMerchantAmt);
                // 商品补贴合计=分账补贴累加
//                groupOrderSheet.setSubsidyAmt(subsidyAmt);
                // 订单总补贴=分账补贴累加+佣金
                enrollOrderSheet.setTotalOrderSubsidy(totalOrderSubsidy);
                // 订单总返现
                enrollOrderSheet.setTotalReturnAmt(totalReturnAmt);

                // 处理报名信息
                enrollInfoHandle(adminOrderItem, enrollOrderSheet);

                enrollOrderSheets.add(enrollOrderSheet);
            }
        }

        TotalEnrollOrderSheet totalEnrollOrderSheet = new TotalEnrollOrderSheet();
        totalEnrollOrderSheet.setOrderNum(orderNum);
        totalEnrollOrderSheet.setVerifyOrderNum(verifyOrderNum);
        totalEnrollOrderSheet.setVerifyOrderAmt(verifyOrderAmt);
        totalEnrollOrderSheet.setTotalOrderSubsidyAmt(totalOrderSubsidyAmt);
        totalEnrollOrderSheet.setTotalCommissionAmt(totalCommissionAmt);
        totalEnrollOrderSheet.setTotalSubsidyAmt(sumSubsidyAmt);
        totalEnrollOrderSheet.setTotalReturnAmt(sumReturnAmt);
        totalEnrollOrderSheet.setOrderUserNum(orderUserNum);
        totalEnrollOrderSheets.add(totalEnrollOrderSheet);
    }

    private void enrollInfoHandle(AdminOrderItemVo adminOrderItem, EnrollOrderSheet enrollOrderSheet) {
        String enrollInfo = adminOrderItem.getEnrollInfo();
        if (!StringUtils.isBlank(enrollInfo)) {
            List<ActiveEnrollInfoSonDto> activeEnrollInfoSonDtos = JSON.parseArray(enrollInfo, ActiveEnrollInfoSonDto.class);
            // 根据排序值正序
            List<ActiveEnrollInfoSonDto> sortActiveEnrollInfoSonDtos = activeEnrollInfoSonDtos.stream()
                    .sorted(Comparator.comparing(ActiveEnrollInfoSonDto::getSort))
                    .collect(Collectors.toList());
            for (int i = 0; i < sortActiveEnrollInfoSonDtos.size(); i++) {
                ActiveEnrollInfoSonDto sortActiveEnrollInfoSonDto = sortActiveEnrollInfoSonDtos.get(i);
                String title = sortActiveEnrollInfoSonDto.getTitle();
                String result = "";

                // 类型(1-填写项，2-单选项，3-多选项)
                Integer type = sortActiveEnrollInfoSonDto.getType();
                if (ActiveEnrollInfoTypeEnum.WRITE.getValue().equals(type)) {
                    // 填写项
                    result = sortActiveEnrollInfoSonDto.getContent();
                } else if (ActiveEnrollInfoTypeEnum.SINGLE.getValue().equals(type)) {
                    // 单选项
                    List<ActiveEnrollInfoOptionDto> activeEnrollInfoOptions = sortActiveEnrollInfoSonDto.getActiveEnrollInfoOptions();
                    if (!CollectionUtils.isEmpty(activeEnrollInfoOptions)) {
                        for (ActiveEnrollInfoOptionDto activeEnrollInfoOption : activeEnrollInfoOptions) {
                            // 勾选状态：1-未勾选，2-已勾选
                            Integer checkState = activeEnrollInfoOption.getCheckState();
                            if (checkState != null && checkState == 2) {
                                result = activeEnrollInfoOption.getOptionText();
                                break;
                            }
                        }
                    }
                } else if (ActiveEnrollInfoTypeEnum.MULTIPLE.getValue().equals(type)) {
                    // 多选项
                    StringBuilder valueStringBuilder = new StringBuilder();
                    List<ActiveEnrollInfoOptionDto> activeEnrollInfoOptions = sortActiveEnrollInfoSonDto.getActiveEnrollInfoOptions();
                    if (!CollectionUtils.isEmpty(activeEnrollInfoOptions)) {
                        for (ActiveEnrollInfoOptionDto activeEnrollInfoOption : activeEnrollInfoOptions) {
                            // 勾选状态：1-未勾选，2-已勾选
                            Integer checkState = activeEnrollInfoOption.getCheckState();
                            if (checkState != null && checkState == 2) {
                                valueStringBuilder.append(activeEnrollInfoOption.getOptionText()).append("、");
                            }
                        }

                        if (valueStringBuilder.length() > 0) {
                            result = valueStringBuilder.deleteCharAt(valueStringBuilder.length() - 1).toString();
                        }
                    }
                }

                if (i == 0) {
                    enrollOrderSheet.setEnrollTitleOne(title);
                    enrollOrderSheet.setEnrollInfoOne(result);
                } else if (i == 1) {
                    enrollOrderSheet.setEnrollTitleTwo(title);
                    enrollOrderSheet.setEnrollInfoTwo(result);
                } else if (i == 2) {
                    enrollOrderSheet.setEnrollTitleThree(title);
                    enrollOrderSheet.setEnrollInfoThree(result);
                } else if (i == 3) {
                    enrollOrderSheet.setEnrollTitleFour(title);
                    enrollOrderSheet.setEnrollInfoFour(result);
                } else if (i == 4) {
                    enrollOrderSheet.setEnrollTitleFive(title);
                    enrollOrderSheet.setEnrollInfoFive(result);
                } else if (i == 5) {
                    enrollOrderSheet.setEnrollTitleSix(title);
                    enrollOrderSheet.setEnrollInfoSix(result);
                } else if (i == 6) {
                    enrollOrderSheet.setEnrollTitleSeven(title);
                    enrollOrderSheet.setEnrollInfoSeven(result);
                } else if (i == 7) {
                    enrollOrderSheet.setEnrollTitleEight(title);
                    enrollOrderSheet.setEnrollInfoEight(result);
                } else if (i == 8) {
                    enrollOrderSheet.setEnrollTitleNine(title);
                    enrollOrderSheet.setEnrollInfoNine(result);
                } else if (i == 9) {
                    enrollOrderSheet.setEnrollTitleTen(title);
                    enrollOrderSheet.setEnrollInfoTen(result);
                }
            }
        }
    }

    private void exportGroupOrder(List<AdminOrderPageVo> list, HttpServletResponse response) {
        // 组装订单明细sheet
        List<GroupOrderSheet> groupOrderSheets = new ArrayList<>();
        // 组装订单汇总sheet
        List<TotalGroupOrderSheet> totalGroupOrderSheets = new ArrayList<>();
        setGroupOrderData(list, groupOrderSheets, totalGroupOrderSheets);

        LocalDateTime nowLocalDateTime = LocalDateUtil.getNowLocalDateTime();
        String fileName = "订单" + LocalDateUtil.toLocalDateTimeToString(nowLocalDateTime, "yyyyMMddHHmmss") + GenerateUtil.getRandomNum(6);

        // 多个sheet的输出需要使用ExcelWriter类，这里想要下载成功，需要输出到OutputStream中
        try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).inMemory(true).build()) {
            // 设置响应类型
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            // 设置编码格式
            response.setCharacterEncoding("utf-8");
            // 设置跨域
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD, PUT");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "*");
            // 设置响应头
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8") + ".xlsx");

            List<List<String>> mergeColDataList = Stream.of(groupOrderSheets.stream().map(GroupOrderSheet::getOrderNo).collect(Collectors.toList())
            ).collect(Collectors.toList());
            List<Integer> mergeColIndexList = Stream.of(0).collect(Collectors.toList());

            WriteSheet groupOrderSheet = EasyExcel.writerSheet(0, "订单明细")
                    .registerWriteHandler(new CustomWriteHandler())
                    .registerWriteHandler(new CustomCellMergeStrategy(mergeColDataList, mergeColIndexList))
                    .head(GroupOrderSheet.class).build();
            excelWriter.write(groupOrderSheets, groupOrderSheet);
            WriteSheet groupOrderSummarySheet = EasyExcel.writerSheet(1, "订单汇总")
                    .registerWriteHandler(new CustomWriteHandler()).head(TotalGroupOrderSheet.class).build();
            excelWriter.write(totalGroupOrderSheets, groupOrderSummarySheet);
        } catch (Exception ioException) {
            ioException.printStackTrace();
        }
    }

    private void setGroupOrderData(List<AdminOrderPageVo> list, List<GroupOrderSheet> groupOrderSheets, List<TotalGroupOrderSheet> totalGroupOrderSheets) {
        // 总订单数
        Integer orderNum = list.size();
        // 核销订单数
        int verifyOrderNum = 0;
        // 核销金额总计=核销的子订单实付金额（分销金额）
        BigDecimal verifyOrderAmt = BigDecimal.ZERO;
        // 订单补贴合计=分账补贴累计
        BigDecimal totalOrderSubsidyAmt = BigDecimal.ZERO;
        // 佣金合计
        BigDecimal totalCommissionAmt = BigDecimal.ZERO;
        // 总补贴金额=分账补贴+佣金
        BigDecimal sumSubsidyAmt = BigDecimal.ZERO;
        // 下单人数
        Integer orderUserNum = list.stream().map(AdminOrderPageVo::getUserId).collect(Collectors.toSet()).size();

        for (AdminOrderPageVo adminOrderPageVo : list) {
            // 推广权益
            String promotionEquity = "";
            // 推广权益类型:0-无,1-其他权益,2-佣金 如果是其他权益,则取otherEquity字段数据,如果是佣金,则取actualCommissionAmt实际全核销佣金金额和totalCommissionAmt总实际佣金
            Integer promotionEquityType = adminOrderPageVo.getPromotionEquityType();
            if (promotionEquityType == 1) {
                promotionEquity = adminOrderPageVo.getOtherEquity();
            } else if (promotionEquityType == 2) {
                promotionEquity = adminOrderPageVo.getActualCommissionAmt().setScale(2, BigDecimal.ROUND_DOWN).toString();
            }

            // 商品本金合计=是所有商品原本的售价合计
            BigDecimal totalMerchantAmt = BigDecimal.ZERO;
            // 商品补贴合计=分账补贴累加
            BigDecimal subsidyAmt = BigDecimal.ZERO;
            // 订单总补贴=分账补贴累加+佣金
            BigDecimal totalOrderSubsidy = BigDecimal.ZERO;
            // 是否是核销订单
            boolean isVerify = false;

            List<AdminOrderItemVo> adminOrderItems = adminOrderPageVo.getAdminOrderItems();
            for (AdminOrderItemVo adminOrderItem : adminOrderItems) {
                BigDecimal goodsPrice = adminOrderItem.getGoodsPrice();
                if (goodsPrice != null && goodsPrice.compareTo(BigDecimal.ZERO) > 0) {
                    totalMerchantAmt = totalMerchantAmt.add(goodsPrice);
                }

                BigDecimal totalSubsidyAmt = adminOrderItem.getTotalSubsidyAmt();
                if (totalSubsidyAmt != null && totalSubsidyAmt.compareTo(BigDecimal.ZERO) > 0) {
                    subsidyAmt = subsidyAmt.add(totalSubsidyAmt);
                    totalOrderSubsidy = totalOrderSubsidy.add(totalSubsidyAmt);
                }

                BigDecimal commissionAmt = adminOrderItem.getCommissionAmt();
                if (commissionAmt != null && commissionAmt.compareTo(BigDecimal.ZERO) > 0) {
//                    totalOrderSubsidy = totalOrderSubsidy.add(commissionAmt);
                    subsidyAmt = subsidyAmt.subtract(commissionAmt);
                }

                Integer subOrderState = adminOrderItem.getSubOrderState();
                if (SubOrderStateEnum.VERIFIED.getValue().equals(subOrderState)) {
                    isVerify = true;
                    verifyOrderAmt = verifyOrderAmt.add(adminOrderItem.getDistributionOrderAmt());
                }
            }

            if (isVerify) {
                verifyOrderNum += 1;
            }
            totalOrderSubsidyAmt = totalOrderSubsidyAmt.add(subsidyAmt);
            totalCommissionAmt = totalCommissionAmt.add(adminOrderPageVo.getActualCommissionAmt());
            sumSubsidyAmt = sumSubsidyAmt.add(totalOrderSubsidy);


            for (AdminOrderItemVo adminOrderItem : adminOrderItems) {
                GroupOrderSheet groupOrderSheet = new GroupOrderSheet();
                groupOrderSheet.setOrderNo(adminOrderPageVo.getOrderNo());
                groupOrderSheet.setActiveTitle(adminOrderPageVo.getActiveTitle());
                groupOrderSheet.setOrderNum(adminOrderPageVo.getOrderNum());

                groupOrderSheet.setSubOrderNo(adminOrderItem.getSubOrderNo());
                groupOrderSheet.setGoodsName(adminOrderItem.getGoodsName());
                groupOrderSheet.setGoodsPrice(adminOrderItem.getGoodsPrice());
                groupOrderSheet.setMerchantAmt(adminOrderItem.getMerchantAmt());
                groupOrderSheet.setTotalSubsidyAmt(adminOrderItem.getTotalSubsidyAmt());
                groupOrderSheet.setSubOrderState(adminOrderItem.getSubOrderState());
                groupOrderSheet.setMerchantName(adminOrderItem.getMerchantShortName());
                groupOrderSheet.setVerifyTime(adminOrderItem.getVerifyTime());

                groupOrderSheet.setOrderNickName(adminOrderPageVo.getOrderNickName());
                groupOrderSheet.setOrderMobile(adminOrderPageVo.getOrderMobile());
                groupOrderSheet.setOrderPrice(adminOrderPageVo.getOrderPrice());
                groupOrderSheet.setDiscountAmt(adminOrderPageVo.getDiscountAmt());
                groupOrderSheet.setRedPacketAmt(adminOrderPageVo.getRedPacketAmt());
                groupOrderSheet.setPayPrice(adminOrderPageVo.getPayPrice());
                groupOrderSheet.setAgentNickName(adminOrderPageVo.getAgentNickName());
                groupOrderSheet.setAgentMobile(adminOrderPageVo.getAgentMobile());
                groupOrderSheet.setOrderState(adminOrderPageVo.getOrderState());

                groupOrderSheet.setOrderTime(adminOrderPageVo.getOrderTime());
                groupOrderSheet.setPayTime(adminOrderPageVo.getPayTime());
                groupOrderSheet.setFinishTime(adminOrderPageVo.getFinishTime());

                // 推广权益
                groupOrderSheet.setPromotionEquity(promotionEquity);

                // 商品本金合计=是所有商品原本的售价合计
                groupOrderSheet.setTotalMerchantAmt(totalMerchantAmt);
                // 商品补贴合计=分账补贴累加
                groupOrderSheet.setSubsidyAmt(subsidyAmt);
                // 订单总补贴=分账补贴累加+佣金
                groupOrderSheet.setTotalOrderSubsidy(totalOrderSubsidy);

                groupOrderSheets.add(groupOrderSheet);
            }
        }

        TotalGroupOrderSheet totalGroupOrderSheet = new TotalGroupOrderSheet();
        totalGroupOrderSheet.setOrderNum(orderNum);
        totalGroupOrderSheet.setVerifyOrderNum(verifyOrderNum);
        totalGroupOrderSheet.setVerifyOrderAmt(verifyOrderAmt);
        totalGroupOrderSheet.setTotalOrderSubsidyAmt(totalOrderSubsidyAmt);
        totalGroupOrderSheet.setTotalCommissionAmt(totalCommissionAmt);
        totalGroupOrderSheet.setTotalSubsidyAmt(sumSubsidyAmt);
        totalGroupOrderSheet.setOrderUserNum(orderUserNum);
        totalGroupOrderSheets.add(totalGroupOrderSheet);
    }


}
